<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Promotion extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();

        //check session
        if( !$this->session->userdata('isLoggedIn') ) {
            redirect(base_url().'admin', 'refresh');
        }

        //Load Helper and library
        $this->load->helper("url");
        $this->load->library("ci_smarty");
        $this->load->helper("text"); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->load->library('pagination'); 

        //Load Model
        $this->load->Model("admin/Mpromotion");

        $this->data['nameAdmin'] = $this->session->userdata('name');

        $this->data['main_selected_cate'] = 'promotion';
    }

    public function listPromotion()
    {
        $this->data['listPromotion'] = $this->Mpromotion->getPromotion();
        $this->ci_smarty->view('backend/list-promotion.tpl',$this->data); 
    }

    public function addPromotion()
    {
        if($this->input->post()){
            // upload image
            $image = $this->Mpromotion->uploadImage();
           
            $param = $this->input->post();

            $param['image'] = $image['file_name'];
                
            $this->Mpromotion->insertPromotion($param); 
            
            redirect(base_url()."admin/list-promotion.html"); 
        }
    }

    public function deletePromotion($promotion_id)
    {
        $promotion = $this->Mpromotion->getpromotionByID($promotion_id);
        
        $this->Mpromotion->deletePromotion($promotion_id); 
        
        $file = APPPATH. '../public/frontend/hinh-anh/quang-cao/'.$promotion['image'];
        unlink($file);
        
        redirect(base_url()."admin/list-promotion.html"); 
    }
}